<template>
  <div id="app">
    <router-view v-wechat-title="$route.meta.title"></router-view>
  </div>
</template>

<script>
import { wxLogin } from "@/api/common";
export default {
  name: "App",
  data() {
    return {};
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      if (window.location.hostname == "localhost") {
        this.$store.dispatch("setUserKey", this.$store.getters.userKey);
      } else {
        this.timeCheck();
      }
    },
    timeCheck() {
      let nowTime = new Date().getTime();
      if (localStorage.getItem("requestTime")) {
        let requestTime = localStorage.getItem("requestTime");
        if (nowTime - requestTime > 60 * 60 * 24 * 1000) {
          localStorage.clear();
          localStorage.setItem("requestTime", nowTime);
          this.weChatAuthorized();
        } else {
          localStorage.setItem("requestTime", nowTime);
        }
      } else {
        localStorage.setItem("requestTime", nowTime);
        this.weChatAuthorized();
      }
    },
    weChatAuthorized() {
      let data = {
        app: "test_member",
        way: "index"
      };
      wxLogin(data)
        .then(res => {
          if (res.data.datas.code_url != "") {
            window.location.href = res.data.datas.code_url;
          } else {
            this.$toast("没有开启微信授权");
          }
        })
        .catch(error => {});
    },
    setForward() {
      let forward = {
        name: this.$route.name,
        query: this.$route.query
      };
      localStorage.setItem("forward", JSON.stringify(forward));
    }
  }
};
</script>

<style>
@import url("./assets/css/reset.css");
@import url("./assets/css/common.css");
</style>
